home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / future / convert / Event.m < prev    next >
Encoding:
Text File  |  1990-07-17  |  13.9 KB  |  779 lines

  1. //
  2. // Event.m
  3. // Copyright (c) 1988, 1989, 1990 by Jiro Nakamura 
  4. // All rights reserved
  5. //
  6. // Implementation definition of class Event. Event handles 
  7. // the database management of Cassandra.
  8. //
  9. // RCS Information
  10. // Revision Number->    $Revision: 2.7 $
  11. // Last Revised->    $Date: 90/03/28 07:29:30 $
  12. //
  13. static char rcsid[] = "$Header: /User/jiro/Programming/Cassandra/src/RCS/Event.m,v 2.7 90/03/28 07:29:30 jiro Exp Locker: jiro $";
  14.  
  15. #import "Event.h"
  16. #import <objc/objc.h>
  17. #import <strings.h>
  18. #import "cass.h"    
  19. #import "calendar.h"
  20. #import "Global.h"
  21. #import "misc.h"
  22. #define NXRunAlertPanel(head, mesg, but1, but3, but4) fprintf(stderr, mesg)
  23.  
  24. // Defining DEBUG for Event can sometimes get very screen messy
  25. // since Cassandra uses it so often
  26. #undef DEBUG
  27.  
  28. #define PROGNAME "NewEvent"
  29.  
  30. @implementation Event
  31.  
  32. + newAt:(const char*) eFile
  33. {
  34.     self = [super new];
  35.     strcpy(eventFile,eFile);
  36.  
  37.     priority = 50;
  38.     destroy = snoozeNo = snoozeInt =0;
  39.     anniversary =  annvSpecial = 0;
  40.     present = next = previous = 0;
  41.     showMessage = TRUE;
  42.     playAlarm = TRUE;
  43.     
  44.     strcpy(alarmSound, ALARMSOUND);
  45.     strcpy(msg,"empty");
  46.     return self;
  47. }
  48.  
  49. + new
  50. {
  51.     fprintf(stderr,"This should never be called!");
  52.     self = [super new];
  53.     return self;
  54. }
  55.  
  56. - free
  57. {
  58. #ifdef DEBUG
  59.     fprintf(stderr,"Event.m:Free ->Closing event!\n");
  60. #endif
  61.     [super free];
  62.     return nil;
  63. }
  64.  
  65. //
  66. //
  67. // Messages that affect how the file is stored on disk
  68. //
  69. //
  70.  
  71. - default: sender
  72. {
  73.     present =  0;
  74.     previous = next = -1;
  75.     destroy =  1;
  76.     anniversary = annvSpecial = snoozeInt = snoozeNo = priority = 0;
  77.     ts.tm_sec =  ts.tm_min =  ts.tm_hour = 0;
  78.     ts.tm_mday = ts.tm_mon =  ts.tm_year = 0;
  79.     ts.tm_wday =  ts.tm_yday =  ts.tm_isdst = 0;
  80.     
  81.     strcpy(msg, "");
  82.     strcpy(alarmSound,[global alarmSound]);
  83.     return self;
  84. }
  85.  
  86.  
  87. - readEvent: (EFileLink) here        /* read itself from the event file */
  88. {
  89.     FILE * source;
  90.     int tmp;
  91.     static char errormsg[128];
  92.         
  93.     #ifdef DEBUG
  94.         fprintf(stderr,    "------------------------\n"
  95.                     "[Event readEvent: %d]\n",here);
  96.     #endif
  97.     
  98.  
  99.     if( here < 0)
  100.         {
  101.         NXRunAlertPanel("File Seek Error", "Cannot seek to a negative number.",
  102.         "OK",NULL,NULL);
  103.         printf("\nSomething is terribly wrong with your event file....\n");
  104.         strcpy(msg, "Illegal event/link value....");
  105.         exit(1);
  106.         }
  107.  
  108.     sprintf(errormsg, "Error occured while trying to read event file %s", eventFile);
  109.     source = fileOpen(eventFile, "r+",errormsg);
  110.  
  111.     sprintf(errormsg,     "An fatal error occured when we were trying "
  112.                 "to seek to event number %d for reading event "
  113.                 "file %s.\n This program has crashed.", 
  114.                 here, eventFile);
  115.     if( fileSeek(source, here, errormsg) == NULL)
  116.         {
  117.         next = previous = -1;
  118.         present = here;
  119.         return nil;
  120.         }
  121.     
  122.     present = fgetnum(source);
  123.     if( present != here)
  124.         {
  125.         next = previous = -1;
  126.         present = here;
  127.         fclose(source);
  128.         return nil;
  129.         }
  130.     else
  131.         {
  132.         previous = fgetnum(source);
  133.         next = fgetnum(source);
  134.         }
  135.     
  136.     if(  (tmp = fscanf( source,     "\n\n"
  137.                     "%d\n%d\n%d\n"
  138.                     "%d\n%d\n"
  139.                     "%d\n0\n"                    
  140.                     "%d\n%d\n%d\n"
  141.                     "%d\n%d\n%d\n"
  142.                     "%d\n%d\n%d\n"
  143.                     "%s\n0\n"
  144.                     "0\n0\n0\n",
  145.                     &priority, &destroy, &anniversary,
  146.                     &snoozeNo, &snoozeInt,
  147.                     &annvSpecial,
  148.                     &ts.tm_sec, &ts.tm_min, &ts.tm_hour,
  149.                     &ts.tm_mday, &ts.tm_mon, &ts.tm_year,
  150.                     &ts.tm_wday, &ts.tm_yday, &ts.tm_isdst,
  151.                     alarmSound
  152.                     )) != 16)
  153.         {
  154.         if( tmp != 15)
  155.             fprintf(stderr, "%s: Error in scanning.... %d != 15\n",
  156.                 PROGNAME,  tmp, here);    // 15 is old version
  157.                 
  158.         if( here == 0 && tmp == 0)
  159.             {
  160.                 strcpy(msg, CASSANDRAVERSION);
  161.                 [self writeEvent: here];
  162.                 fprintf(stderr, "%s: Event list not initialized. Initializing....\n",
  163.                     PROGNAME);                    
  164.                 fclose(source);
  165.                 return nil;
  166.             }
  167.                 
  168.         if( tmp == 0)
  169.             {
  170.             strcpy(msg, "Empty Record");
  171.             fclose(source);
  172.             return nil;
  173.             }
  174.     
  175.         }
  176.     fgets(msg,MESSAGE_SIZE,source);
  177.                 
  178.     ts.tm_wday = wday(ts.tm_mday, ts.tm_mon+1, 1900+ts.tm_year);
  179.     ts.tm_yday = yday(ts.tm_mday, ts.tm_mon+1, ts.tm_year+1900);
  180.     
  181.     playAlarm = TRUE;
  182.     deleteSound = FALSE;
  183.     
  184.     if( alarmSound[0] == '-')
  185.         {
  186.         char buf[255];
  187.         
  188.         strcpy(buf, alarmSound);
  189.         strcpy(alarmSound, buf+1);
  190.         playAlarm = FALSE;
  191.         }
  192.     else
  193.         {
  194.         if( alarmSound[0] == '?')
  195.             {
  196.             char buf[255];
  197.         
  198.             strcpy(buf, alarmSound);
  199.             strcpy(alarmSound, buf+1);
  200.             deleteSound = TRUE;
  201.             }
  202.         }
  203.     
  204.         
  205.         
  206.     if( msg[0] == ';')
  207.         {
  208.         char buf[MESSAGE_SIZE];
  209.         
  210.         strcpy(buf, msg);
  211.         strcpy(msg, buf+1);
  212.         showMessage = FALSE;
  213.         }
  214.     else
  215.         showMessage = TRUE;
  216.         
  217.         
  218.     fclose(source);
  219.     return self;
  220. }
  221.                 
  222. - firstEvent
  223. {
  224.     #ifdef DEBUG
  225.         fprintf(stderr,"Reading in the first event.\n");
  226.     #endif
  227.  
  228.     [self readEvent : 0];
  229.     if( [self next] == 0)
  230.         return self;
  231.     [self readEvent : [self next]];
  232.     return self;
  233. }    
  234.     
  235. - writeEvent: (EFileLink) here    
  236. /* write itself into the event file using present as its index */
  237. {
  238.     FILE *source;
  239.     char errormsg[256], bufAlarm[256], bufMessage[256];
  240.         
  241.     if( here < 0)
  242.         {
  243.         NXRunAlertPanel( "Write Error", 
  244.                 "We can't write to a negative event number.",
  245.                 "OK",NULL,NULL);
  246.         return nil;
  247.         }
  248.         
  249.     #ifdef DEBUG
  250.         fprintf(stderr,    "------------------------\n"
  251.                 "[Event writeEvent: %d]: Opening event with "
  252.                 "<%s>!\n",here, eventFile);
  253.     #endif
  254.  
  255.     sprintf(errormsg, "Fatal error when trying to write to event file %s", 
  256.         eventFile);
  257.     source = fileOpen(eventFile, "r+",errormsg);
  258.     
  259.     sprintf(errormsg, "%s: An fatal error occured when we were trying to "
  260.             "seek to event number %d for writing in event file %s.\n"
  261.             " This program has crashed.", PROGNAME, here, eventFile);
  262.     fileSeek(source, here, errormsg);
  263.     
  264.     present = here;
  265.     
  266.     if( ts.tm_year > 1900)
  267.         ts.tm_year -= 1900;
  268.  
  269.     if( playAlarm == FALSE)
  270.         {        
  271.         strcpy(bufAlarm, "-");
  272.         strcat(bufAlarm,alarmSound);
  273.         }
  274.     else
  275.         {
  276.         if( deleteSound == TRUE )
  277.             {
  278.             strcpy(bufAlarm, "?");
  279.             strcat(bufAlarm,alarmSound);
  280.             }
  281.         }
  282.  
  283.     if( showMessage == FALSE)
  284.         {        
  285.         strcpy(bufMessage, ";");
  286.         strcat(bufMessage, msg);
  287.         }
  288.  
  289.  
  290.     if(fprintf( source,         "%d\n%d\n%d\n"
  291.                     "0\n0\n"
  292.                     "%d\n%d\n%d\n"
  293.                     "%d\n%d\n"
  294.                     "%d\n0\n"
  295.                     "%d\n%d\n%d\n"
  296.                     "%d\n%d\n%d\n"
  297.                     "%d\n%d\n%d\n"
  298.                     "%s\n0\n"
  299.                     "0\n0\n0\n"
  300.                     "%s\n",
  301.                     present, previous, next,
  302.                     priority,destroy, anniversary,
  303.                     snoozeNo, snoozeInt,
  304.                     annvSpecial,
  305.                     ts.tm_sec, ts.tm_min, ts.tm_hour,
  306.                     ts.tm_mday, ts.tm_mon, ts.tm_year,
  307.                     ts.tm_wday, ts.tm_yday, ts.tm_isdst,
  308.                     bufAlarm, bufMessage) == -1)
  309.         {
  310.         fprintf(stderr, "%s: Event.m:   Error occured writing file.",
  311.              PROGNAME);
  312.         strcpy(msg, "Error occured in writing out Elink number.\n\n");
  313.         fclose(source);
  314.         return nil;
  315.         }
  316.     
  317.         #ifdef DEBUG
  318.             fprintf(stderr, "%s: Wrote out Efilelink.\n  Here = %d, "
  319.                 "Previous = %d, Next=%d\n", PROGNAME,
  320.                 here, previous, next);
  321.             fprintf(stderr, "%s: Date:   time = %d:%d, day= %d, "
  322.                 "month = %d, year = %d.\n\n",PROGNAME,
  323.                 ts.tm_hour, ts.tm_min, ts.tm_mday, ts.tm_mon, 
  324.                 ts.tm_year);
  325.             fprintf(stderr,"%s: AlarmSound =%s\n", PROGNAME, bufAlarm);
  326.         #endif
  327.         
  328.     fclose(source);
  329.     return self;
  330. }
  331.  
  332. - (EFileLink) insertEvent
  333. {
  334.     return [self insertEventFrom : 1];
  335. }
  336.  
  337.  
  338.  
  339. /* insert itself into the file with current day as index */
  340. /* and starting from <here> in searching for a open deleted space */
  341. - (EFileLink) insertEventFrom : (EFileLink) here
  342. {
  343.     id event, step;
  344.     int loop;
  345.     
  346.     // This may take a while, so use the system wait cursor
  347.  
  348.     // First things first, we must check the format of our time structure since 
  349.     // we have given other functions full freedom to scew it up
  350.  
  351.     fixTmStructure( &ts);
  352.             
  353.     event = [Event newAt:eventFile];
  354.     step = [Event newAt:eventFile];
  355.     [event readEvent:0];
  356.     for( [event readEvent: [event next]]; 
  357.           [event present] != 0 && timeCompare([event time], &ts) < 1;
  358.           [event readEvent:[event next]])
  359.               {
  360.               #ifdef DEBUG
  361.             fprintf(stderr, "From %d looping onto %d\n",[event present],[event next]);
  362.         #endif
  363.         }
  364.     #ifdef DEBUG    
  365.         fprintf(stderr, "inserting between %d <- event -> %d.\n",[event previous], [event present]);
  366.     #endif
  367.     
  368.     previous = [event previous];
  369.     next = [event present];
  370.     
  371.     [step setNext: 0];
  372.     for( loop = here; !([step next] == -1 && [step previous] == -1); [step readEvent: loop++])
  373.         {
  374.         #ifdef DEBUG
  375.             fprintf(stderr, "reading at %d.\n",loop);
  376.         #endif
  377.         }
  378.         
  379.     present = [step present];
  380.     
  381.     [self writeEvent: present];
  382.     
  383.     [step readEvent: previous];
  384.     [step setNext : present];
  385.     [step writeEvent:previous];
  386.     
  387.     [step readEvent: next];
  388.     [step setPrevious : present];
  389.     [step writeEvent:next];
  390.     
  391.     [step free];
  392.     [event free];
  393.     #ifdef DEBUG
  394.         fprintf(stderr,"successfully inserted %d <- %d -> %d\n\n",[self previous],[self present], [self next]);
  395.     #endif
  396.  
  397.  
  398.     // Finished, restore the cur    
  399.     return( [self present] );
  400. }
  401.  
  402. /* delete itself from the file with present as index */
  403. - deleteEvent  : (EFileLink) here 
  404. {
  405.     id previousEvent, nextEvent;
  406.     
  407.     #ifdef DEBUG
  408.         fprintf(stderr, "Deleting <%d>\n\n",here);
  409.     #endif
  410.     
  411.     if( here <= 0)
  412.         {
  413.         NXRunAlertPanel(NULL,"You cannot delete link 0 or below. OK?",
  414.                  "Shucks...",NULL,NULL);
  415.         fprintf(stderr,"Can't delete the header.  Here = %d.\n\n",
  416.              here);
  417.         return nil;
  418.         }
  419.     
  420.     previousEvent= [Event newAt: eventFile];
  421.     nextEvent = [Event newAt:eventFile];
  422.     
  423.     [previousEvent readEvent  :here];
  424.     if( ( [previousEvent previous] == -1  )   || 
  425.         ( [previousEvent next]  == -1  )        )
  426.         {
  427.         /*   link was already deleted, so we can't delete it..... */
  428.         strcpy(msg, "Can't delete link because it's already been deleted!?!\n\n");
  429.         return nil;
  430.         }
  431.     
  432.     /* Set the previous_link of the next link */
  433.     /* to the one of the previous one */                    
  434.     [nextEvent readEvent :[previousEvent next]];    
  435.     [nextEvent setPrevious  : [previousEvent previous]];            
  436.     [nextEvent writeEvent  :[previousEvent next]];    
  437.     
  438.     /* do the same confusing spiel for previous */
  439.     [nextEvent readEvent  :[previousEvent previous] ];    
  440.     [nextEvent setNext  : [previousEvent next]];
  441.     [nextEvent writeEvent  :[previousEvent previous] ];
  442.     
  443.     /* cancel out the header to finally kill it */
  444.     /* and write it out */
  445.     [previousEvent setNext  : -1];    
  446.     [previousEvent setPrevious  :-1];    
  447.     [previousEvent writeEvent  :here];    
  448.     
  449.     #ifdef DEBUG
  450.         fprintf(stderr, "Record %d successfully deleted.\n\n", 
  451.             here);
  452.     #endif
  453.     
  454.     [previousEvent free];
  455.     [nextEvent free];
  456.     return self;
  457. }
  458.  
  459. /* delete if it is not an anniversary event, update otherwise */
  460. - (int) murderEvent : (EFileLink) here
  461. {
  462.     Event *event;
  463.     
  464.     #ifdef DEBUG
  465.         fprintf(stderr,"Arrggh! Help! I'm getting murdered! (says Event <%d>)\n\n",here);
  466.     #endif
  467.     
  468.     event = [Event newAt :eventFile];
  469.     [event readEvent : here];        //    Read the event in
  470.     [event deleteEvent : here];        //    Delete the event
  471.         
  472.     [event setTime: fixAnniversary( [event time], [event anniversary], [event annvSpecial])];
  473.                                 //     Fix the next event time and reinsert it
  474.                                 
  475.     // Use the same EFilelink space that we deleted, this saves 
  476.     // us time and maybe some confusion 
  477.     return( [event insertEventFrom:here]  );
  478. }
  479.  
  480.  
  481. - (EFileLink) present
  482. {
  483.     return present;
  484. }
  485.  
  486. - (EFileLink) previous
  487. {
  488.     return previous;
  489. }
  490.  
  491. - (EFileLink) next
  492. {
  493.     return next;
  494. }
  495.  
  496. - setPresent : (EFileLink) apresent
  497. {
  498.     present = apresent;
  499.     return self;
  500. }
  501. - setPrevious  : (EFileLink) aprevious
  502. {
  503.     previous =aprevious;
  504.     return self;
  505. }
  506.  
  507. - setNext  : (EFileLink) anext
  508. {
  509.     next = anext;
  510.     return self;
  511. }
  512.  
  513. - (struct tm *) time
  514. {
  515.     return &ts;
  516. }
  517.  
  518. - setTime : (struct tm *) time
  519. {    
  520.     ts = *time;
  521.     return self;
  522. }
  523.  
  524. - setMday : (int) x
  525. {    
  526.     ts.tm_mday = x;
  527.     return self;
  528. }
  529.  
  530. - (int) mday;
  531. {
  532.     return( ts.tm_mday);
  533. }
  534.  
  535. - setMon: (int) x
  536. {
  537.     ts.tm_mon = x;
  538.     return self;
  539. }
  540.  
  541. - (int) mon
  542. {
  543.     return (ts.tm_mon);
  544. }
  545.  
  546. - setYear: (int) x
  547. {
  548.     ts.tm_year = x;
  549.     return self;
  550. }
  551.  
  552. - (int) year
  553. {
  554.     return (ts.tm_year);
  555. }
  556.  
  557. - setHour: (int) x
  558. {
  559.     ts.tm_hour = x;
  560.     return self;
  561. }
  562.  
  563. - (int) hour
  564. {
  565.     return (ts.tm_hour);
  566. }
  567.  
  568. - setMin: (int) x
  569. {
  570.     ts.tm_min = x;
  571.     return self;
  572. }
  573.  
  574. - (int) min
  575. {
  576.     return(ts.tm_min);
  577. }
  578.  
  579. - setSec: (int) x
  580. {
  581.     ts.tm_sec = x;
  582.     return self;
  583. }
  584.  
  585. - (int) sec
  586. {
  587.     return(ts.tm_sec);
  588. }
  589.  
  590. - (int) destroy
  591. {
  592.     return destroy;
  593. }
  594.  
  595. - setDestroy : (int) dst
  596. {
  597.     destroy = dst;
  598.     return self;
  599. }
  600.  
  601.  
  602. - (int) anniversary
  603. {
  604.     return anniversary;
  605. }
  606.  
  607. - setAnniversary : (int) anv
  608. {
  609.     anniversary = anv;
  610.     return self;
  611. }
  612.  
  613.  
  614. - (int) annvSpecial
  615. {
  616.     return annvSpecial;
  617. }
  618.  
  619. - setAnnvSpecial : (int) anvS
  620. {
  621.     annvSpecial = anvS;
  622.     return self;
  623. }
  624.  
  625. - (int) snoozeNo
  626. {
  627.     return snoozeNo;
  628. }
  629.  
  630. - setSnoozeNo : (int) sn
  631. {
  632.     snoozeNo = sn;
  633.     return self;
  634. }
  635.  
  636.  
  637. - (int) snoozeInt
  638. {
  639.     return snoozeInt;
  640. }
  641.  
  642. - setSnoozeInt : (int) si
  643. {
  644.     snoozeInt = si;
  645.     return self;
  646. }
  647.  
  648. - (int) priority
  649. {
  650.     return priority;
  651. }
  652.  
  653. - setPriority : (int) si
  654. {
  655.     priority = si;
  656.     return self;
  657. }
  658.  
  659. - (char *) alarmSound
  660. {
  661.     return alarmSound;
  662. }
  663.  
  664. - setAlarmSound : (char *) aS
  665. {    
  666.     strcpy( alarmSound, aS);
  667.     return self;
  668. }
  669.  
  670. - setPlayAlarm: (BOOL) aFlag
  671. {
  672.     playAlarm = aFlag;
  673.     return self;
  674. }
  675.  
  676.  
  677. - (BOOL) playAlarm
  678. {
  679.     return playAlarm;
  680. }
  681.  
  682. - setShowMessage: (BOOL) aFlag
  683. {
  684.     showMessage = aFlag;
  685.     return self;
  686. }
  687.  
  688.  
  689.  
  690. - (BOOL) deleteSound
  691. {
  692.     return deleteSound;
  693. }
  694.  
  695. - setDeleteSound: (BOOL) aFlag
  696. {
  697.     deleteSound = aFlag;
  698.     return self;
  699. }
  700.  
  701. - (BOOL) showMessage
  702. {
  703.     return showMessage;
  704. }
  705.  
  706.  
  707. - (char *) message
  708. {
  709.     return msg;
  710. }
  711.  
  712.  
  713. - setMessage : (char *) message
  714. {    
  715.     strcpy( msg, message);
  716.     return self;
  717. }
  718.  
  719. - setWday : (int) wd
  720. {
  721.     ts.tm_wday = wd;
  722.     return self;
  723. }
  724.  
  725. - setYday : (int) yd
  726. {
  727.     ts.tm_yday = yd;
  728.     return self;
  729. }
  730.  
  731. - (int) wday
  732. {
  733.     return ts.tm_wday;
  734. }
  735.  
  736. - (int) yday
  737. {
  738.     return ts.tm_yday;
  739. }
  740. @end        
  741.  
  742.  
  743. struct tm *fixAnniversary( struct tm *time, int mode, int sub)
  744. {
  745. fprintf(stderr,"Fixing weekly special: %s, %d, %d", ascMyTime(time, YES, NO), mode, sub);
  746.  
  747.     switch( (int) mode  / 100)      
  748.         {
  749.         /* Don't worry about overflows as we add intervals to */
  750.         /* our dates, insertEvent is smart enough to handle it */
  751.         case 0:
  752.             return time;    /* No anniversary */
  753.         case 1:            /* If it is a day anniversary */
  754.             time->tm_mday+=  (mode - 100);
  755.             break;
  756.         case 2:            /* Weekly anniversary */
  757.             time -> tm_mday +=  ((mode - 200) * 7 );
  758.             break;
  759.         case 3:            /* Monthly anniversary */
  760.             time-> tm_mon += (mode - 300);
  761.             break;
  762.         case 4:            /* Yearly anniversary */
  763.             time -> tm_year +=  (mode - 400);
  764.             break;
  765.         case 5:            /* Special weekly */
  766.             break;
  767.         default:
  768.              fprintf(stderr,
  769.                 "%s: I do not recognize anniversary event: %d.\n", PROGNAME,
  770.                  mode);
  771.              return time;
  772.         }
  773.  
  774.     fixTmStructure( time);
  775.     return time;
  776. }
  777.  
  778.  
  779.